package com.ly.fn.biz.ins.srv.api.register;

import com.ly.fn.biz.ins.srv.api.common.ServiceConfig;
import com.ly.fn.inf.util.SleepUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class MqsStatusCheck implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private Environment environment;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        String port = environment.getProperty("server.port");
        String baseUrl = "http://127.0.0.1:" + port;
        if (!ServiceConfig.instance.isMqsEnabled()) {
            String stopUrl = baseUrl + "/mqs-xpro-stop";
            String startUrl = baseUrl + "/mqs-xpro-start";
            System.out.println("click to stop mqs:" + stopUrl);
            System.out.println("click to start mqs:" + startUrl);

            new Thread(() -> {
                SleepUtil.sleep(1000 * 3);
                try {
                    String resultData = restTemplate.getForObject(stopUrl, String.class);

                    System.out.println("stop mqs:" + resultData);
                } finally {
                    //todo
                }
            }).start();
        }

        System.out.println("============ auto api ============");
        System.out.println("doc api = " + baseUrl + "/doc.html");
        System.out.println("============ auto api ============");

    }
}
